/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 * @version 1.0
 */
package shop.controller.manage.contract;
import shop.core.common.bean.JSONResult;
import shop.core.common.dao.page.PagerModel;
import shop.core.util.KeyValueHelper;
import shop.core.Services;
import shop.core.util.MD5;
import shop.services.manage.contract.ContractService;
import shop.services.manage.contract.bean.Contract;
import shop.core.BaseController;
import shop.core.RequestHolder;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import shop.services.manage.proxy.ProxyService;
import shop.services.manage.proxy.bean.Proxy;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @类名称：ContractAction
 * @创建人：wzl
 * @创建时间：2018-05-24 下午13:52:35
 * @版本号：1.0
 * @描述：
 */
@Controller
@RequestMapping("/manage/contract/")
public class ContractAction extends BaseController<Contract> {
    private static final Logger logger = LoggerFactory.getLogger(ContractAction.class);
    @Autowired
    private ContractService contractService;
    @Autowired
    private ProxyService proxyService;
    private static final String page_toProjectList = "/manage/project/projectList";
    private static final String page_toList = "/manage/contract/contractList";
    private static final String page_toAdd = "/manage/contract/contractAdd";
    private static final String page_toEdit = "/manage/contract/contractEdit";
    public ContractService getService() {
        return contractService;
    }

    private ContractAction(){
        super.page_toList = page_toList;
        super.page_toAdd = page_toAdd;
        super.page_toEdit = page_toEdit;
    }

    /**
     * 跳转到股东列表页面
     *
     * @return
     */
    @RequestMapping("toProjectList")
    public String toProjectList() {
        return page_toProjectList;
    }

    /**
     * 分页查詢项目列表
     *
     * @param request 请求
     * @param contract    对象参数
     * @return 分页数据模型
     */
    @RequestMapping("selectContractPage")
    @ResponseBody
    public PagerModel selectContractPage(HttpServletRequest request, Contract contract) {
        int offset = 0;
        int pageSize = 10;
        int start = Integer.parseInt(request.getParameter("start"));
        int length = Integer.parseInt(request.getParameter("length"));
        if (start > 0)
            offset = start;
        if (length > 0)
            pageSize = length;
        contract.setOffset(offset);
        contract.setPageSize(pageSize);
        //分页查詢项目列表
        PagerModel pager = contractService.selectContractPage(contract);
        pager.setRecordsTotal(pager.getTotal());
        pager.setRecordsFiltered(pager.getTotal());
        return pager;
    }

    /**
     * 分页查詢客户项目列表
     *
     * @param request 请求
     * @param contract    对象参数
     * @return 分页数据模型
     */
    @RequestMapping("selectCustomerContractPage")
    @ResponseBody
    public PagerModel selectCustomerContractPage(HttpServletRequest request, Contract contract) {
        int offset = 0;
        int pageSize = 10;
        int start = Integer.parseInt(request.getParameter("start"));
        int length = Integer.parseInt(request.getParameter("length"));
        if (start > 0){
            offset = start;
        }
        if (length > 0){
            pageSize = length;
        }
        contract.setOffset(offset);
        contract.setPageSize(pageSize);
        //分页查詢客户项目列表
        PagerModel pager = contractService.selectCustomerContractPage(contract);
        pager.setRecordsTotal(pager.getTotal());
        pager.setRecordsFiltered(pager.getTotal());
        return pager;
    }

    /**
     * 批量修改项目状态
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "updateStatus", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult updateStatus(@RequestParam(value = "ids[]") String[] ids,String status) throws Exception {
        JSONResult jsonResult = new JSONResult();
        for(int i=0;i<ids.length;i++){
            Contract contract=new Contract();
            contract.setId(ids[i]);
            contract.setStatus(status);
            contractService.update(contract);
        }
        return jsonResult;
    }

    /**
     * 新增合同
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "insertContract", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertContract(Contract contract) throws Exception {
        JSONResult jsonResult = new JSONResult();
        contract.setContractGetMoney(contract.getContractPayMoney());
        int id=contractService.insert(contract);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String sno=String.valueOf(id);
        if(sno.length()==1){
            sno="00"+sno;
        }else if(sno.length()==2){
            sno="0"+sno;
        }else{

        }
        String createdate = sdf.format(date)+sno;
        Contract contractInfo=new Contract();
        contractInfo.setId(String.valueOf(id));
        contractInfo.setSno(createdate);
        contractService.update(contractInfo);
        jsonResult.setData(id);
        return jsonResult;
    }

    /**
     * 查询合同剩余总金额
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("selectSurplusMoney")
    @ResponseBody
    public JSONResult selectSurplusMoney(Contract contract) throws Exception {
        contract=contractService.selectOne(contract);
        BigDecimal surplusMoney=contract.getContractMoney();
        Proxy proxy=new Proxy();
        proxy.setContractID(contract.getId());
        List<Proxy> proxyList = proxyService.selectList(proxy);
        if(proxyList!=null&&proxyList.size()>0){
            for(Proxy proxyInfo :proxyList){
                surplusMoney=surplusMoney.subtract(proxyInfo.getProxyMoney());
            }
        }
        JSONResult jsonResult = new JSONResult();
        jsonResult.setData(surplusMoney);
        return jsonResult;
    }

    /**
     * 查询合同剩余预付款金额
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("selectSurplusPayMoney")
    @ResponseBody
    public JSONResult selectSurplusPayMoney(Contract contract) throws Exception {
        contract=contractService.selectOne(contract);
        BigDecimal surplusPayMoney=contract.getContractPayMoney();
        Proxy proxy=new Proxy();
        proxy.setContractID(contract.getId());
        List<Proxy> proxyList = proxyService.selectList(proxy);
        if(proxyList!=null&&proxyList.size()>0){
            for(Proxy proxyInfo :proxyList){
                surplusPayMoney=surplusPayMoney.subtract(proxyInfo.getProxyPayMoney());
            }
        }
        JSONResult jsonResult = new JSONResult();
        jsonResult.setData(surplusPayMoney);
        return jsonResult;
    }

    /**
     * 分页查询项目金额统计
     *
     * @param request 请求
     * @param contract    对象参数
     * @return 分页数据模型
     */
    @RequestMapping("selectContractCountPage")
    @ResponseBody
    public PagerModel selectContractCountPage(HttpServletRequest request, Contract contract) {
        int offset = 0;
        int pageSize = 10;
        int start = Integer.parseInt(request.getParameter("start"));
        int length = Integer.parseInt(request.getParameter("length"));
        if (start > 0){
            offset = start;
        }
        if (length > 0){
            pageSize = length;
        }
        contract.setOffset(offset);
        contract.setPageSize(pageSize);
        //分页查詢项目列表
        PagerModel pager = contractService.selectContractCountPage(contract);
        List<Contract> rsList=pager.getList();
        for(Contract contractInfo :rsList){
            Contract oneContract=new Contract();
            oneContract.setCustomerID(contractInfo.getCustomerID());
            List<Contract> ContractList=contractService.selectList(oneContract);
            contractInfo.setContractAmount(ContractList.size());
        }
        pager.setRecordsTotal(pager.getTotal());
        pager.setRecordsFiltered(pager.getTotal());
        return pager;
    }

    /**
     * 分页查询客户所属项目金额统计
     *
     * @param request 请求
     * @param contract    对象参数
     * @return 分页数据模型
     */
    @RequestMapping("selectCustomerContractCountPage")
    @ResponseBody
    public PagerModel selectCustomerContractCountPage(HttpServletRequest request, Contract contract) {
        int offset = 0;
        int pageSize = 10;
        int start = Integer.parseInt(request.getParameter("start"));
        int length = Integer.parseInt(request.getParameter("length"));
        if (start > 0)
            offset = start;
        if (length > 0)
            pageSize = length;
        contract.setOffset(offset);
        contract.setPageSize(pageSize);
        //分页查詢项目列表
        PagerModel pager = contractService.selectCustomerContractCountPage(contract);
        pager.setRecordsTotal(pager.getTotal());
        pager.setRecordsFiltered(pager.getTotal());
        return pager;
    }

}
